export const getWeeks = (year: number, month: number) => {
	var d = new Date()
	d.setFullYear(year, month - 1, 1)
	var w1 = d.getDay()
	if (w1 == 0) w1 = 7
	d.setFullYear(year, month, 0)
	var dd = d.getDate()
	let d1
	if (w1 != 1) d1 = 7 - w1 + 2
	else d1 = 1
	let week_count = Math.ceil((dd - d1 + 1) / 7)
	return week_count
}

export const getWeekDates = (year: number, month: number, weekday: number) => {
	const d = new Date()
	// 该月第一天
	d.setFullYear(year, month - 1, 1)
	let w1 = d.getDay()
	if (w1 == 0) w1 = 7
	// 该月天数
	d.setFullYear(year, month, 0)
	const dd = d.getDate()
	// 第一个周一
	let d1
	if (w1 != 1) d1 = 7 - w1 + 2
	else d1 = 1
	const monday = d1 + (weekday - 1) * 7
	const sunday = monday + 6
	const from = new Date(year, month - 1, monday)
	let to
	if (sunday <= dd) {
		to = new Date(year, month - 1, sunday)
	} else {
		d.setFullYear(year, month - 1, sunday)
		let days = d.getDate()
		to = new Date(d.getFullYear(), d.getMonth(), days)
	}

	const weekDate: Date[] = []
	weekDate.push(from)
	let t = new Date(from.getTime())
	for (let i = 0; i < 5; i++) {
		t = new Date(t.getTime() + 24 * 60 * 60 * 1000)
		weekDate.push(t)
	}
	weekDate.push(to)
	return weekDate
}

export const getMonthWeek = (
	year: number,
	month: number,
	dateOfMonth: number
) => {
	const date = new Date(year, month - 1, dateOfMonth)
	console.log(year, month, dateOfMonth)
	let w = date.getDay()
	if (w == 0) {
		w = 7
	}
	const d = date.getDate()
	let r = Math.ceil((d + 6 - w) / 7)
	// 如果当月的第一天不是周一,则减1
	date.setFullYear(year, month, 1)
	return date.getDay() === 1 ? r : r - 1
}

export const isToDay = (date: Date) => {
	const now = new Date()
	return (
		date.getFullYear() == now.getFullYear() &&
		date.getMonth() == now.getMonth() &&
		date.getDate() == now.getDate()
	)
}

export const isToMonth = (date: Date) => {
	const now = new Date()
	return (
		date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth()
	)
}
